const { ccclass, property } = cc._decorator;

@ccclass
export default class TipPanel extends cc.Component {
    static Instance: TipPanel;

    @property(cc.Node)
    parent: cc.Node = null;
    @property(cc.Node)
    Prefab: cc.Node = null;

    array: cc.Node[] = new Array();

    onLoad() {
        TipPanel.Instance = this;
    }

    start() {}

    Show(content: string, duration = 0.6) {
        const node: cc.Node = cc.instantiate(this.Prefab);
        let label = node.getComponentInChildren(cc.Label);
        if (!label) {
            label = node.addComponent(cc.Label);
        }

        this.DestroyAll();
        this.array.push(node);

        label.string = content;
        node.parent = this.parent;
        node.setPosition(0, 100);
        node.active = true;

        node.opacity = 0;
        cc.tween(node)
            .to(0.6, { opacity: 255 }, { easing: "" })
            .delay(duration)
            .to(1, { opacity: 0 }, { easing: "" })
            .call(() => {
                this.DestroyAll();
            })
            .start();
    }

    DestroyAll() {
        this.array.forEach((element) => {
            element.destroy();
        });
        this.array = new Array();
    }
}

/** 调试 */
window["XXL_TipPanel"] = TipPanel;
